package com.web.oa.controller;

import com.web.oa.pojo.*;
import com.web.oa.service.EmployeeService;
import com.web.oa.service.SysService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private SysService sysService;

    //用户管理
    //查找用户列表
    @RequestMapping("/findUserList")
    public String findUserList(String userId,Model model){
        //先找所有角色
        List<SysRole>allrole=sysService.findAllRole();
        //查询用户和角色列表
        List<EmployeeCustom>employees=employeeService.findUserAndRoleList();
        model.addAttribute("allRoles",allrole);
        model.addAttribute("userList",employees);
        return "userlist";
    }

    //用户管理中的查看权限
    @RequestMapping("/viewPermissionByUser")
    @ResponseBody
    public SysRole viewPermissionByUser(String userName) {
        SysRole sysRole = sysService.findRolesAndPermissionsByUserId(userName);

        System.out.println(sysRole.getName()+"," +sysRole.getPermissionList());
        return sysRole;
    }
    //角色添加
    @RequestMapping("/toAddRole")
    public String toAddRole(Model model,Employee employee){
        //获取所有的菜单
        List<MenuTree>allMenu=sysService.findAllMenu();
        //获取所有菜单权限
        List<SysPermission>sysPermissions=sysService.findSysPermission();
        //获取所有角色和权限
        List<SysRole> allroleandpermission=sysService.findallRoleAndPermission();
        model.addAttribute("allPermissions",allMenu);
        model.addAttribute("menuTypes",sysPermissions);
        model.addAttribute("roleAndPermissionsList",allroleandpermission);
        return "rolelist";
    }

    //在userlist.jsp这个前端页面
    //授予用户角色
    @RequestMapping("/assignRole")
    @ResponseBody
    public Map<String, String> assignRole(String roleId, String userId) {
        Map<String, String> map = new HashMap<>();
        try {
            employeeService.updateEmployeeRole(roleId, userId);
            map.put("msg", "分配权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "分配权限失败");
        }
        return map;
    }

    //在rolelist.jsp这个前端页面
    @RequestMapping("/saveRoleAndPermissions")
    public String saveRoleAndPermissions(SysRole role,int[] permissionIds) {
        //设置role主键，使用uuid
        String uuid = UUID.randomUUID().toString();
        role.setId(uuid);
        //默认可用
        role.setAvailable("1");

        sysService.addRoleAndPermissions(role, permissionIds);

        return "redirect:/toAddRole";
    }

    //在rolelist.jsp这个前端页面
    @RequestMapping("/saveSubmitPermission")
    public String saveSubmitPermission(SysPermission permission) {
        if (permission.getAvailable() == null) {
            permission.setAvailable("0");
        }
        sysService.addSysPermission(permission);
        return "redirect:/toAddRole";
    }

    //在permissionlist.jsp这个前端页面
    @RequestMapping("/updateRoleAndPermission")
    public String updateRoleAndPermission(String roleId,int[] permissionIds) {
        sysService.updateRoleAndPermissions(roleId, permissionIds);
        return "redirect:/findRoles";
    }

    //在userlist.jsp这个前端页面
    @RequestMapping("/saveUser")
    public String saveUser(Employee employee, Model model){
        System.out.println("employee:"+employee);
        int emp=this.employeeService.saveUser(employee);
        model.addAttribute("permissionForm",emp);
        return "redirect:findUserList";
    }

    //在permissionlist.jsp这个前端页面
    @RequestMapping("/findRoles")
    public ModelAndView findRoles() {
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        List<SysRole> roles = sysService.findAllRoles();
        List<MenuTree> allMenuAndPermissions = sysService.getAllMenuAndPermision();

        ModelAndView mv = new ModelAndView();
        mv.addObject("allRoles", roles);
        mv.addObject("activeUser",activeUser);
        mv.addObject("allMenuAndPermissions", allMenuAndPermissions);

        mv.setViewName("permissionlist");
        return mv;
    }

    //在userlist.jsp这个前端页面
    //根据员工级别查找下一级别主管
    @RequestMapping("/findNextManager")
    @ResponseBody
    public List<Employee> findNextManager(int level) {
        level++; //加一，表示下一个级别
        List<Employee> list = employeeService.findEmployeeByLevel(level);
        System.out.println(list);
        return list;

    }

    //在permissionlist.jsp这个前端页面
    @RequestMapping("/loadMyPermissions")
    @ResponseBody
    public List<SysPermission> loadMyPermissions(String roleId) {
        List<SysPermission> list = sysService.findPermissionsByRoleId(roleId);

        for (SysPermission sysPermission : list) {
            System.out.println(sysPermission.getId()+","+sysPermission.getType()+"\n"+sysPermission.getName() + "," + sysPermission.getUrl()+","+sysPermission.getPercode());
        }
        return list;
    }

}
